<template>
	<el-dialog
		title="角色管理"
		:visible.sync="increased"
		:close-on-click-modal="false"
		append-to-body
		:close-on-press-escape="false"
		:before-close="newlyCancel"
	>
		<el-form
			ref="newlyform"
			:model="newlyform"
			label-width="80px"
			:rules="rules"
		>
			<el-form-item label="角色名称" prop="newly">
				<el-input v-model="newlyform.newly"></el-input>
			</el-form-item>
			<el-form-item label="是否启用">
				<el-switch v-model="delivery"></el-switch>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" size="small" @click="newly('newlyform')"
					>保存</el-button
				>
				<el-button size="small" @click="newlyCancel('newlyform')"
					>取消</el-button
				>
			</el-form-item>
		</el-form>
	</el-dialog>
</template>

<script>
import { roleInsert, roleNameCheck } from '@/http/api.js'
export default {
	name: 'Newly',
	props: {
		increased: {
			type: Boolean,
			default: false,
		},
		// delivery: {
		// 	type: Boolean,
		// 	default: false,
		// },
	},
	data() {
		var spCodeValidator = this.spCodeFun
		return {
			delivery: false,
			rules: {
				newly: [
					{ required: true, message: '不能为空', trigger: 'blur' },
					{ max: 10, message: '不允许超过10个字', trigger: 'blur' },
					{ validator: spCodeValidator, trigger: 'blur' },
				],
			},
			newlyform: {
				newly: '',
			},
		}
	},
	methods: {
		// 新增弹框
		newly(formName) {
			this.$refs[formName].validate(valid => {
				if (valid) {
					if (this.delivery) {
						this.roleSave = 0
					} else {
						this.roleSave = 1
					}
					let roleName = this.newlyform.roleName
					console.log('新增', roleName)
					roleInsert({
						roleName: this.newlyform.newly,
						parentUuid: 0,
						whetherEnable: this.roleSave,
					}).then(data => {
						if (data.data.head.code == 200) {
							this.$message({
								message: '保存成功',
								type: 'success',
							})
						}
					})
					this.newlyform = {
						roleName: '',
					}
					this.delivery = false
					let dialogValue = {
						verdict: false,
						typeValue: 1,
					}
					this.$emit('newlyClick', dialogValue)
				} else {
					// this.$message.error('保存失败')
					return false
				}
			})
		},
		newlyCancel() {
			this.newlyform = {
				roleName: '',
			}
			// this.increased = false
			let dialogValue = {
				verdict: false,
				typeValue: 0,
			}
			this.$emit('newlyClick', dialogValue)
			this.$nextTick(() => {
				this.$refs['newlyform'].clearValidate()
			})
			this.delivery = false
		},
		//新增查重
		spCodeFun(rule, value, callback) {
			roleNameCheck({ parentUuid: 0, roleName: value })
				.then(({ data }) => {
					if (data.head.code == 200) {
						callback()
					}
				})
				.catch(({ data }) => {
					if (data.head.code == 400) {
						callback(new Error('账号不能重复'))
						return
					} else {
						callback()
					}
				})
		},
	},
}
</script>

<style></style>
